package com.know.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Friends_links;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;

import my.db.QueryHelper;

public class LinksDao {
	/**
	 * 读取友情链接列表
	 */
	public List<Friends_links> list(){
		String sql="select *from friends_links";
		return QueryHelper.query(Friends_links.class, sql);
	}
	/**
	 * 分页查询
	 */
	public Record findPage(Page<Friends_links> page) {
		Record record = new Record();
		String sql = "select * from friends_links";
		if(StringUtils.isNotBlank(page.getOrderBy())) {
			sql += String.format(" order by %s %s", page.getOrderBy(), page.getOrder());
		}
		
		List<Friends_links> list = QueryHelper.queryPage(Friends_links.class, sql, page.getPageNo(), page.getPageSize());

	
		//统计总数count
		String sql2 = sql.toLowerCase().replaceFirst("select .*? from", "select count(*) from");
		long count = QueryHelper.stat(sql2);
		
		record.setCount(count);
		record.setData(list);
		
		return record;
	
	}
	/**
	 * 保存友情链接
	 * @return 
	 */
	public long save(Friends_links link) {
		String sql= "insert into friends_links values(null,?,?,?,?)";//友情链接表
		String sql1="select friends_link_id from friends_links order by friends_link_id DESC LIMIT 1 ";

	QueryHelper.update(sql,link.getFriend_links(),link.getFriend_link_name(),link.getFriend_link_description(),link.getFriend_link_logo());
	return QueryHelper.stat(sql1);
	}
	/**
	 * 修改友情链接
	 */
	public void update(Friends_links link) {
		String sql="UPDATE friends_links SET  friend_links=？, friend_link_name=？, friend_link_description=？, friend_link_logo=？ WHERE (friends_link_id=？)";
		QueryHelper.update(sql,link.getFriend_links(),link.getFriend_link_name(),link.getFriend_link_description(),link.getFriend_link_logo(),link.getFriend_link_id());
	}
	/**
	 * 根据ID查找
	 */
	public Friends_links finById(long parseLong) {
		String sql="select *from friends_links where friend_link_id=?";
		Friends_links fl=QueryHelper.queryUnique(Friends_links.class, sql, parseLong);
		return fl;
	}
	/**
	 * 删除链接
	 */
	public void del(long id) {
		String sql="delete from friends_links where friend_link_id =?";
		QueryHelper.update(sql, id);
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public void multiDel(String ids) {
		String sql = "delete from friends_links where friend_link_id in (" + ids + ")";
		QueryHelper.update(sql);
		
	}
	
}
